package com.crm.service.system.impl;

import cn.hutool.core.util.ObjectUtil;
import com.crm.dto.EmpPageResult;
import com.crm.dto.QueryEmpTypeDto;
import com.crm.mapper.system.DeptMapper;
import com.crm.mapper.system.EmpMapper;
import com.crm.pojo.Dept;
import com.crm.service.system.EmpService;
import com.crm.vo.TbUserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EmpServiceImpl
 * @Description
 * @Author 19632
 * @Date 2024/10/30 10:40
 * @Version
 **/
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;


    @Override
    public EmpPageResult queryAll(QueryEmpTypeDto dto) {

        dto.check();
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        Map<String, Object> params = dto.getParams();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate beginTime = ObjectUtil.isEmpty(params)?null:(LocalDate.parse((String)params.get("beginTime"), formatter));
        LocalDate endTime =ObjectUtil.isEmpty(params)?null:(LocalDate.parse((String)params.get("endTime"), formatter));

        dto.setBeginTime(beginTime);
        dto.setEndTime(endTime);

        Page<TbUserVo>page=deptMapper.page(dto);
        List<TbUserVo> result = page.getResult();
        List<TbUserVo>tbUserVoList=new ArrayList<>();
        for (TbUserVo tbUserVo : result) {
            Integer deptId = tbUserVo.getDeptId();
            Dept byDeptId = deptMapper.getByDeptId(deptId);
            tbUserVo.setDept(byDeptId);
            tbUserVoList.add(tbUserVo);
        }
        return new EmpPageResult(tbUserVoList, page.getTotal());
    }
}
